package com.base.cn.platform.os.controller.mobile.user.center.integral;

import com.alibaba.fastjson.JSON;
import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.msg.Message;
import com.base.cn.platform.os.common.utils.action.ActionKey;
import com.base.cn.platform.os.entity.integralgood.*;
import com.base.cn.platform.os.entity.user.integral.CusUserIntegral;
import com.base.cn.platform.os.entity.user.integral.CusUserIntegralRecord;
import com.base.cn.platform.os.entity.user.integral.CusUserIntegralRule;
import com.base.cn.platform.os.entity.user.integral.IntegralRule;
import com.base.cn.platform.os.service.manage.integralgood.IntegralCardVoucherService;
import com.base.cn.platform.os.service.manage.integralgood.IntegralExchangeRecordService;
import com.base.cn.platform.os.service.manage.integralgood.IntegralGoodService;
import com.base.cn.platform.os.service.manage.user.integral.CusUserIntegralRecordService;
import com.base.cn.platform.os.service.manage.user.integral.CusUserIntegralRuleService;
import com.base.cn.platform.os.service.manage.user.integral.CusUserIntegralService;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * 我的积分
 * @auth 268xue
 * @date 2019/10/21
 */
@Controller
@RequestMapping("/mobile/uc/integral/")
public class MobileUserIntegralController extends MobileBaseController {
    @Autowired
    private   CusUserIntegralService cusUserIntegralService;
    @Autowired
   private CusUserIntegralRecordService cusUserIntegralRecordService;
    @Autowired
    private IntegralGoodService integralGoodService;
    @Autowired
    private CusUserIntegralRuleService cusUserIntegralRuleService;
    @Autowired
    private IntegralExchangeRecordService integralExchangeRecordService;

    @Autowired
    private IntegralCardVoucherService integralCardVoucherService;
    /**
     * 我的积分首页
     */
    @RequestMapping("/integralIndex")
    public String userIntegralIndex(Model model, HttpServletRequest request) {
        //获取用户积分
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> UserIntegral = cusUserIntegralService.queryUserIntegralByUserId(loginUserId);
        IntegralRule userIntegralRuleRatto= cusUserIntegralRuleService.getUserIntegralRuleByKey(ActionKey.RATIO);
        IntegralRule userIntegralRuleByInvite = cusUserIntegralRuleService.getUserIntegralRuleByKey(ActionKey.INVITE);
        IntegralRule userIntegralRuleByProtect = cusUserIntegralRuleService.getUserIntegralRuleByKey(ActionKey.PROTECT);
        IntegralRule userIntegralRuleByValidtime = cusUserIntegralRuleService.getUserIntegralRuleByKey(ActionKey.VALIDTIME);

        TreeMap<String, String> contextTreeMap1 = userIntegralRuleByInvite.getContextTreeMap();
        TreeMap<String, String> contextTreeMap2 = userIntegralRuleRatto.getContextTreeMap();
        TreeMap<String, String> contextTreeMap3 = userIntegralRuleByProtect.getContextTreeMap();
        TreeMap<String, String> contextTreeMap4 = userIntegralRuleByValidtime.getContextTreeMap();
        model.addAttribute("userIntegralRuleByInvite",contextTreeMap1);
        model.addAttribute("userIntegralRuleRatto",contextTreeMap2);
        model.addAttribute("userIntegralRuleByProtect",contextTreeMap3);
        model.addAttribute("userIntegralRuleByValidtime",contextTreeMap4);

        model.addAttribute("userIntegral",UserIntegral);


        return resultPage("/user/center/integral/u-integral");
    }

    /**
     * 获取积分列表
     */
    @RequestMapping("/integralAjax")
    public String userIntegralIndex(Model model,HttpServletRequest request, @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                    @RequestBody CusUserIntegralRecord cusUserIntegralRecord) {
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
        cusUserIntegralRecord.setUserId(loginUserId);
        Map<String, Object> stringObjectMap = cusUserIntegralRecordService.queryCusUserIntegralRecordListPage(currentPage, cusUserIntegralRecord, false);
        model.addAttribute("dataMap",stringObjectMap);
        return resultPage("/user/center/integral/integral-list");
    }

    /**
     * 积分礼品首页
     */
    @RequestMapping("/integralGiftIndex")
    public String integralGiftIndex(Model model,HttpServletRequest request,@RequestParam(value = "type",required = false ,defaultValue = "1") Integer type) {
        //获取用户积分
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
        String nickname = webMobileLoginUtil.getNickname(request);
        Map<String, Object> userIntegral = cusUserIntegralService.queryUserIntegralByUserId(loginUserId);
        String avatar = webMobileLoginUtil.getAvatar(request);
        model.addAttribute("avatar",avatar);
        model.addAttribute("nickname",nickname);
        model.addAttribute("userIntegral",userIntegral);
        model.addAttribute("type",type);
        return resultPage("/user/center/integral/u-integral-gift");
    }


    /**
     * 获取积分的商品
     */
    @RequestMapping("/integralShopAjax")
    public String integralShopAjax(Model model, @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                    @RequestBody IntegralGood integralGood) {
        integralGood.setStatus(3);
        Map listPage = integralGoodService.findListPage(currentPage, integralGood);
        model.addAttribute("dataMap",listPage);
        return resultPage("/user/center/integral/u-integral-shop");
    }



    /**
     * 积分礼品详情
     */
    @RequestMapping("/integralGiftInfo")
    public String integralGiftInfo( HttpServletRequest request,Model model,@RequestParam BigDecimal id) {
        Map byIdEntity = integralGoodService.findByIdEntity(id, false);
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> userIntegral = cusUserIntegralService.queryUserIntegralByUserId(loginUserId);

        model.addAttribute("userIntegral",userIntegral);
        model.addAttribute("shopInfo",byIdEntity);
        return resultPage("/user/center/integral/u-integral-gift-info");
    }

    /**
     * 积分规则说明
     */
    @RequestMapping("/integralRuleInfo")
    public String integralRuleInfo(Model model) {
        Map<String, IntegralRule> rule = cusUserIntegralRuleService.integralRuleData("RULE");
        model.addAttribute("integralRuleMap",rule);
        return resultPage("/user/center/integral/u-integral-rule-info");
    }


    /**
     * 兑换商品
     */
    @RequestMapping("/exchangeIntegralShop")
    @ResponseBody
    public Map exchangeIntegralShop(HttpServletRequest request,@RequestParam BigDecimal id) {
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
        return  integralGoodService.exchangeIntegralShop(loginUserId, id);
    }




    /**
     * 获取兑换记录
     */
    @RequestMapping("/listIntegralExchangeAjax")
    public String listIntegralExchangeAjax(HttpServletRequest request,Model model, @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                           @RequestBody IntegralExchangeRecordExpand integralExchangeRecord) {
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
        integralExchangeRecord.setSetIntegralGood(true);
        integralExchangeRecord.setUserId(loginUserId);
        Map listPage = integralExchangeRecordService.findListPage(currentPage, integralExchangeRecord);
        model.addAttribute("dataMap",listPage);
        return resultPage("/user/center/integral/u-integral-exchangerecord");
    }
    /**
     * 获取兑换记录
     */
    @RequestMapping("/redeemedCash")
    @ResponseBody
    public Map redeemedCash(HttpServletRequest request,@RequestParam(value = "cash",defaultValue = "0") Integer cash) {
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);

        return  integralGoodService.redeemedCash(loginUserId,cash);

    }

    /**
     * 查看卡密
     */
    @RequestMapping("/checkTheCard")
    @ResponseBody
    public Map checkTheCard(HttpServletRequest request,@RequestParam BigDecimal id) {
        BigDecimal loginUserId = webMobileLoginUtil.getLoginUserId(request);
        IntegralCardVoucherExpand integralCardVoucher = new  IntegralCardVoucherExpand();
        integralCardVoucher.setUserId(loginUserId);
        integralCardVoucher.setId(id);
        return integralCardVoucherService.geiInfoIntegralCardVoucher(integralCardVoucher);
    }

}
